package com.fmatos.carcontrol.view;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;

import com.fmatos.carcontrol.R;
import com.fmatos.carcontrol.bluetooth.BluetoothManager;

public class BluetoothListAdapter  extends BaseAdapter{

	private static final String TAG = BluetoothListAdapter.class.getSimpleName();
	private Activity activity;
	private Context context;
	private List<BluetoothDevice> devices;
	private LayoutInflater mIInflater;
	
	private String preferredMac;
	
	public BluetoothListAdapter(Activity activity) {
	
		this.activity = activity;
		context = activity;
		mIInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		initDevicesList();
		
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(activity);

		preferredMac = prefs.getString(CarPreferences.PREFERRED_MAC,"");
		Log.i(TAG,"Preferred mac is " + preferredMac);
		
	}
	
	private void initDevicesList() {
		BluetoothManager manager = new BluetoothManager(context);
		devices = new ArrayList<BluetoothDevice>();
		devices.addAll(manager.scanPairedDevices());
		Collections.sort(devices,new Comparator<BluetoothDevice>() {

			@Override
			public int compare(BluetoothDevice lhs, BluetoothDevice rhs) {
				if (lhs.getName() != null  ) {
					return lhs.getName().compareTo(rhs.getName());
				} else {
					return 0;
				}
			}});
		
	}
	
	@Override
	public int getCount() {
		return devices.size();
	}
	@Override
	public BluetoothDevice getItem(int position) {
		return devices.get(position);
	}
	@Override
	public long getItemId(int position) {

		return position;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = mIInflater.inflate(R.layout.row_bluetooth_picker, parent, false);
		
		TextView niceName = (TextView) view.findViewById(R.id.txtNiceName);
		TextView macAddress = (TextView) view.findViewById(R.id.txtMac);
		Switch preferred = (Switch) view.findViewById(R.id.switch1);
		
		final BluetoothDevice device = getItem(position);
		niceName.setText(device.getName());
		macAddress.setText(device.getAddress());
		if ( isPreferred(device.getAddress()) ) {
			preferred.setChecked(true);
		} else {
			preferred.setChecked(false);
		}
		
		preferred.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

				 if( isChecked ) {
					 preferredMac = device.getAddress();
				 } else {
					 preferredMac = "";
				 }
				 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
				 Editor editor = prefs.edit();
				 editor.putString(CarPreferences.PREFERRED_MAC, preferredMac);
				 editor.apply();
				 notifyDataSetChanged();			
			}
		});
		
		return view;
		
	}

	private boolean isPreferred(String mac) {

		return preferredMac.equals(mac);
	}
}
